<?php
class Login extends CI_Controller {

	 public function __construct()
       {
            parent::__construct();
        	$this->output->enable_profiler(PROFILER);                         $this -> em = $this -> doctrine -> em;
       }	


	public function index() {

		$data['main_content'] = "login/login_form";
		$this -> load -> view("login/template", $data);
	}

	function validate_credentials() {
		$user = $this->authenticate($this -> input -> post('username'), $this -> input -> post('password'));

		if ($user)// if the user's credentials validated...
		{		    
			$data = array('username' => $user->getUsername(), 'user_id' => $user->getId(),'is_logged_in' => true);
			$this -> session -> set_userdata($data);
			redirect('user/index');
		} else// incorrect username or password
		{
			$this -> session -> set_flashdata('message', array('title' => '<i class="icon-lock"></i> Access denied', 'content' => 'You are not allowed to access the admin section of this site, please contact the administrator', 'type' => ALERT_TYPE_ERROR));
			redirect('login/index');
		}
	}                public function authenticate($username,$password)    {                return  $users = $this -> em -> getRepository('models\User') ->findOneBy(array('username' => $username,'password' => md5($password)));            }    

	function logout() {
		$this -> session -> sess_destroy();
		redirect('login/index');
	}

}
?>